package com.sq.partner.manager.service.impl.credit;

import java.sql.SQLException;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sq.partner.manager.dao.IGatewayTradProdPlanDao;
import com.sq.partner.manager.dao.TradProdPropDao;
import com.sq.partner.manager.dao.credit.CreditProductDao;
import com.sq.partner.manager.dao.credit.CreditRiskDao;
import com.sq.partner.manager.mybatis.model.CreditProduct;
import com.sq.partner.manager.mybatis.model.CreditRisk;
import com.sq.partner.manager.mybatis.model.NlifeProd;
import com.sq.partner.manager.mybatis.model.NlifeRisk;
import com.sq.partner.manager.mybatis.model.TradProdPlan;
import com.sq.partner.manager.mybatis.model.TradProdProp;
import com.sq.partner.manager.service.credit.CreditProductService;
import com.sq.partner.manager.util.Page;
import com.sq.partner.manager.util.StrUtil;

@Service
public class CreditProductServiceImpl implements CreditProductService {

	@Resource
	private CreditProductDao creditProductDao;
	@Resource
	private TradProdPropDao tradProdPropDao;
	@Resource
	private IGatewayTradProdPlanDao iGatewayTradProdPlanDao;
	@Resource
	private CreditRiskDao creditRiskDao;
	
	@Override
	public TradProdPlan saveProd(CreditProduct credit) throws Exception {
		String temcreditProd =creditProductDao.findMaxCreditTradProdCode(credit.getProdType());
		if(null!=temcreditProd){
			long prodCode= Long.parseLong(temcreditProd.substring(4))+1;
			String nextProdCodeVal = credit.getProdType().concat(String.format("%05d", prodCode));
			credit.setProdCode(nextProdCodeVal);
		}else{
			credit.setProdCode(credit.getProdType()+"00001");
		}
		Date sysdate = new Date();
		credit.setInsertTime(sysdate);
		boolean saveProd = creditProductDao.saveProd(credit);
		if(saveProd){
			/*if(StrUtil.isNotEmpty(credit.getEpolicyCode())){
				*//**模板代码非空，新增电子保单模板*//*
				TradProdProp prodProp=new TradProdProp();
				prodProp.setProdValue(credit.getEpolicyCode());
				prodProp.setePolicyTitle(credit.getEpolicyTitle());
				prodProp.setTradProdCod(credit.getProdCode());
				tradProdPropDao.addProdProp(prodProp);
			}*/
			TradProdPlan tradprodplan = new TradProdPlan();
			tradprodplan.setTradProdCode(credit.getProdCode());
			tradprodplan.setTradPlanCode(credit.getProdType());
			tradprodplan.setInsType("120001");
			tradprodplan.setRequired("Y");
			iGatewayTradProdPlanDao.saveTradProdPlan(tradprodplan);
			CreditRisk creditRisk = new CreditRisk();
			creditRisk.setProdCode(credit.getProdCode());
			creditRisk.setRiskCode(credit.getProdType());
			creditRisk.setRiskName(credit.getProdName());
			creditRisk.setStatus("1");
			creditRisk.setcField1(credit.getProdType()+"01");
			creditRisk.setcField2(credit.getProdType()+"01");
			creditRisk.setnField1("1");
			creditRiskDao.addCreditRisk(creditRisk);
			return tradprodplan;
		}
		//新增产品,清交易系统缓存
		//cleanTradSystemCache(credit.getProdCode());
		return null;
	}

	@Override
	public boolean updateProd(CreditProduct credit) throws Exception {
		return creditProductDao.updateProd(credit);
		//更新产品,清交易系统缓存
		//cleanTradSystemCache(credit.getProdCode());
	}

	@Override
	public void deleteProd(String prodCode) throws Exception {
		TradProdPlan tradprodplan = new TradProdPlan();
		tradprodplan.setTradProdCode(prodCode);
		tradprodplan.setTradPlanCode(prodCode.substring(0, 4));
		iGatewayTradProdPlanDao.deleteTradProdPlan(tradprodplan);
		creditRiskDao.delCreditRiskByProdId(prodCode);
		creditProductDao.deleteProd(prodCode);
		//更新产品,清交易系统缓存
		//cleanTradSystemCache(credit.getProdCode());
	}

	@Override
	public CreditProduct findProdInfoById(String prodCode) throws SQLException {
		return creditProductDao.queryProdInfoById(prodCode);
	}

	@Override
	public Page<CreditProduct> queryPage(Page<CreditProduct> page)
			throws SQLException {
		return creditProductDao.queryPage(page);
	}
	
	@Override
	public List<CreditProduct> impCreditProd(Map<String, Object>map) throws Exception {
		
		return creditProductDao.impCreditProd(map);
	}

	@Override
	public List<CreditRisk> impCreditRisk(Map<String, Object> map)
			throws Exception {
		return creditRiskDao.impCreditRisk(map);
	}

}
